package com.ruoyi.eat.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.eat.domain.OrderItem;
import com.ruoyi.eat.mapper.OrderMapper;
import com.ruoyi.eat.domain.Order;
import com.ruoyi.eat.service.IOrderService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.CollectionUtils;

/**
 * 订单Service业务层处理
 *
 */
@Service
public class CarOrderServiceImpl implements IOrderService {
    @Autowired
    private OrderMapper carOrderMapper;

    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public Order selectCarOrderById(String id) {
        return carOrderMapper.selectCarOrderById(id);
    }

    /**
     * 查询订单列表
     *
     * @param carOrder 订单
     * @return 订单
     */
    @Override
    public List<Order> selectCarOrderList(Order carOrder) {
        return carOrderMapper.selectCarOrderList(carOrder);
    }

    /**
     * 新增订单
     *
     * @param carOrder 订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCarOrder(Order carOrder) {
        carOrder.setCreateTime(DateUtils.getNowDate());

        carOrder.setOrderName(getOrderName(carOrder.getCarOrderItemList()));
        int rows = carOrderMapper.insertCarOrder(carOrder);
        insertCarOrderItem(carOrder);
        return rows;
    }

    /**
     * 获取订单名称
     *
     * @param carOrderItemList 订单
     * @return 结果
     */
    private String getOrderName(List<OrderItem> carOrderItemList) {
        StringBuffer sb = new StringBuffer();
        int i = 0;
        if (!CollectionUtils.isEmpty(carOrderItemList)) {

            for (OrderItem carOrderItem : carOrderItemList) {
                String goodName = carOrderItem.getGoodName();
                if (i == 0) {
                    sb.append(goodName.split(" ")[0]);
                    i++;
                } else {
                    sb.append(" | ").append(goodName.split(" ")[0]);
                }
            }
        }
        String orderName = sb.toString();
        return orderName.length() < 60 ? orderName : orderName.substring(0, 59) + "...";
    }

    /**
     * 修改订单
     *
     * @param carOrder 订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCarOrder(Order carOrder, boolean justUpdate) {
        carOrder.setUpdateTime(DateUtils.getNowDate());
        return carOrderMapper.updateCarOrder(carOrder);
    }


    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCarOrderByIds(String ids) {

        return carOrderMapper.deleteCarOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCarOrderById(String id) {

        return carOrderMapper.deleteCarOrderById(id);
    }



    @Override
    public Map<String, String> countMap(Long userId) {
        return carOrderMapper.countMap(userId) ;
    }

    /**
     * 新增清单明细信息
     * 同时扣减库存
     *
     * @param carOrder 订单对象
     */
    public void insertCarOrderItem(Order carOrder) {
        List<OrderItem> carOrderItemList = carOrder.getCarOrderItemList();
        String  id = carOrder.getId();
        if (StringUtils.isNotNull(carOrderItemList)) {
            List<OrderItem> list = new ArrayList<OrderItem>();
            for (OrderItem carOrderItem : carOrderItemList) {
                carOrderItem.setOrderId(id);
                list.add(carOrderItem);
            }
            if (list.size() > 0) {
                carOrderMapper.batchCarOrderItem(list);
            }
        }
    }
}
